Friday, September 4, 2020

STM32F103R6 SysTick Two-Digit Multiplexing Display and Push Button

In this example, I create a counter using a push button and a two-digit multiplexing display. The display is common cathode type. I use STM32 SysTick to drive either display and push button. The program is written using STM32CubeIDE. I use Proteus 8 to simulate this example program.

STM32F103R6 SysTick Two-Digit Multiplexing Display and Push Button
Simulating Program In Proteus

 The display is a two-digit common cathode type. I use two 74HC04 Hex inverter to drive the commons. However we can use two switching NPN transistors to drive the commons.

Only one single Port B is required. Between PB0 and PB9 are digital output pins. While PB15 is an digital input pin.

STM32F103R6 SysTick Two-Digit Multiplexing Display and Push Button
Code Configuration Wizard

Code Configuration Tool allow us to select any peripheral before the source code is generated. Each digits are activated around 5 Milli-seconds. SW2 switch is active low. It's activated by pressing event without depending on SysTick.

  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file : main.c
  5.   * @brief : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2022 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   * opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22.  
  23.  
  24. /* Private function prototypes -----------------------------------------------*/
  25. void SystemClock_Config(void);
  26. static void MX_GPIO_Init(void);
  27. /* USER CODE BEGIN PFP */
  28.  
  29. const char dCathode[10] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
  30. const char SW2=15;
  31. volatile unsigned int msCnt=0,msSsd=0,pressCnt=0;
  32.  
  33. int main(void)
  34. {
  35.  
  36. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  37. HAL_Init();
  38.  
  39. /* Configure the system clock */
  40. SystemClock_Config();
  41.  
  42. /* Initialize all configured peripherals */
  43. MX_GPIO_Init();
  44.  
  45. /*SysTick Configuration*/
  46. SystemCoreClockUpdate();
  47. /*Generate interrupt for 100 us*/
  48. SysTick_Config(SystemCoreClock/10000);
  49. SysTick ->CTRL=0;
  50. SysTick ->VAL=0;
  51. SysTick ->CTRL=(SysTick_CTRL_TICKINT_Msk //Enable SysTick Exception
  52. |SysTick_CTRL_ENABLE_Msk //Enable SysTick system timer
  53. |SysTick_CTRL_CLKSOURCE_Msk); //Use process clock source
  54.  
  55. while (1)
  56. {
  57. switch(msSsd){
  58. case 0:
  59. GPIOB ->ODR &=~(1<<8) ;
  60. GPIOB ->ODR = dCathode[pressCnt/10]|(1<<SW2);
  61. GPIOB ->ODR |= GPIO_ODR_ODR8;
  62. break;
  63.  
  64. case 5:
  65. GPIOB ->ODR &=~(1<<9) ;
  66. GPIOB ->ODR = dCathode[pressCnt%10]|(1<<SW2);
  67. GPIOB ->ODR |= GPIO_ODR_ODR9;
  68. break;
  69. }
  70.  
  71. if((GPIOB->IDR&(1<<SW2))==0){
  72. if(msCnt>=200){
  73. msCnt=0;
  74. pressCnt+=1;
  75. }
  76. }
  77.  
  78. }
  79. /* USER CODE END 3 */
  80. }
  81.  
  82. void SysTick_Handler(void)
  83. {
  84.  
  85. HAL_IncTick();
  86.  
  87. if(uwTick>=10){
  88. msCnt+=1;
  89. msSsd+=1;
  90. uwTick=0;
  91. }
  92.  
  93. if(msCnt>=500) msCnt=0;
  94.  
  95. if(msSsd>10) msSsd=0;
  96.  
  97. }
  98.  
  99. void SystemClock_Config(void)
  100. {
  101. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  102. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  103.  
  104. /** Initializes the RCC Oscillators according to the specified parameters
  105.   * in the RCC_OscInitTypeDef structure.
  106.   */
  107. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  108. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  109. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  110. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  111. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  112. {
  113. Error_Handler();
  114. }
  115. /** Initializes the CPU, AHB and APB buses clocks
  116.   */
  117. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  118. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  119. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  120. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  121. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  122. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  123.  
  124. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  125. {
  126. Error_Handler();
  127. }
  128. }
  129.  
  130. /**
  131.   * @brief GPIO Initialization Function
  132.   * @param None
  133.   * @retval None
  134.   */
  135. static void MX_GPIO_Init(void)
  136. {
  137. GPIO_InitTypeDef GPIO_InitStruct = {0};
  138.  
  139. /* GPIO Ports Clock Enable */
  140. __HAL_RCC_GPIOB_CLK_ENABLE();
  141. __HAL_RCC_GPIOA_CLK_ENABLE();
  142.  
  143. /*Configure GPIO pin Output Level */
  144. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  145. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET);
  146.  
  147. /*Configure GPIO pins : PB0 PB1 PB2 PB3
  148.   PB4 PB5 PB6 PB7 */
  149. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  150. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
  151. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  152. GPIO_InitStruct.Pull = GPIO_NOPULL;
  153. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  154. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  155.  
  156. /*Configure GPIO pin : PB15 */
  157. GPIO_InitStruct.Pin = GPIO_PIN_15;
  158. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  159. GPIO_InitStruct.Pull = GPIO_PULLUP;
  160. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  161.  
  162. }
  163.  
  164. /* USER CODE BEGIN 4 */
  165.  
  166. /* USER CODE END 4 */
  167.  
  168. /**
  169.   * @brief This function is executed in case of error occurrence.
  170.   * @retval None
  171.   */
  172. void Error_Handler(void)
  173. {
  174. /* USER CODE BEGIN Error_Handler_Debug */
  175. /* User can add his own implementation to report the HAL error return state */
  176. __disable_irq();
  177. while (1)
  178. {
  179. }
  180. /* USER CODE END Error_Handler_Debug */
  181. }
  182.  
  183. #ifdef USE_FULL_ASSERT
  184. /**
  185.   * @brief Reports the name of the source file and the source line number
  186.   * where the assert_param error has occurred.
  187.   * @param file: pointer to the source file name
  188.   * @param line: assert_param error line source number
  189.   * @retval None
  190.   */
  191. void assert_failed(uint8_t *file, uint32_t line)
  192. {
  193. /* USER CODE BEGIN 6 */
  194. /* User can add his own implementation to report the file name and line number,
  195.   ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  196. /* USER CODE END 6 */
  197. }
  198. #endif /* USE_FULL_ASSERT */
  199.  
  200. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  201.  

Click here to download its source file.

 For other similar posts please check,

  1. Getting Started With STM32F103C8T6 Module with STM32CubeIDE
  2.  STM32F103C8T6 Blue Pill SysTick and Multiplexing Display Example
  3.  STM32F103C8T6 Blue Pill Switch And Multiplexing Display Interface Using SysTick
  4.  STM32F103C8T6 Blue Pill SysTick LED Blinking
  5. STM32F103R6 Common Anode Seven Segments Display Example 
  6. STM32F103R6 Common Anode Seven Segments Display And Switch Interfacing 
  7. STM32F103R6 Simple 2-Digit Multiplexing Display And Switch Example 
  8. STM32F103R6 SysTick And Digital Clock Example 
  9. STM32F103R6 SysTick Two-Digit Multiplexing Display and Push Button
  10. LED Blinking With STM32F103R6 Using SysTick

No comments:

Post a Comment